package com.fjrz.bbxwj.mode.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.fjrz.bbxwj.entity.*
import com.fjrz.bbxwj.mode.api.*
import com.fjrz.bbxwj.mode.manager.UploadProgress
import com.module.lemlin.base.BaseViewModel
import com.module.lemlin.mode.ViewModelSubscribe
import com.module.lemlin.rxhttp.HttpServiceData
import com.module.lemlin.rxhttp.HttpServiceResponse
import io.reactivex.rxjava3.disposables.Disposable
import rxhttp.wrapper.entity.Progress
import java.io.File


class ReportViewModel(application: Application) : BaseViewModel(application) {


    val reportTypeLiveData = MutableLiveData<HttpServiceResponse<ReportTypeResponse>>()

    /**
     * 举报类型列表
     * */
    fun reportTypeList() {
        val disposable: Disposable =
            reportTypeList(ViewModelSubscribe(reportTypeLiveData))
        dispose.add(disposable)
    }

    val baseLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()

    /**
     * 举报提交
     * */
    fun reportSubmit(
        videoId: Int, typeId: Int, content: String, images: String
    ) {
        val disposable: Disposable =
            reportSubmit(videoId.toString(), typeId.toString(), content, images, ViewModelSubscribe(baseLiveData))
        dispose.add(disposable)
    }


    val uploadLiveData = MutableLiveData<HttpServiceResponse<UploadResponse>>()
    val progressLiveData = MutableLiveData<Progress>()

    /**
     * 上传视频
     * */
    fun commonUpload(path: String, type: UploadResponse.Type = UploadResponse.Type.FILE_NULL) {
        if (path.isNullOrEmpty())
            return
        val progress = object : UploadProgress {
            override fun onProgress(progress: Progress) {
                progressLiveData.postValue(progress)
            }
        }
        val listener = object : ViewModelSubscribe<UploadResponse>(uploadLiveData) {
            override fun onSuccess(resp: UploadResponse) {
                super.onSuccess(resp.apply { this.type = type })
            }
        }
        val disposable: Disposable =
            com.fjrz.bbxwj.mode.manager.commonUpload(File(path), progress, listener)
        dispose.add(disposable)
    }
}